package team.niit.dao.impl;

import cn.hutool.core.util.StrUtil;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import team.niit.dao.NotificationDAO;
import team.niit.entity.NotificationEntity;
import team.niit.util.SessionFactoryUtil;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: JSF
 * @Package_name: team.niit.dao.impl
 * @Date: 2021/10/26 20:10
 * Explain:
 */
public class NotificationDAOImpl implements NotificationDAO {

    private Session session;
    private Transaction tx;

    @Override
    public void insertNotification(NotificationEntity notificationEntity) {
        try {
            session = SessionFactoryUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.save(notificationEntity);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            session.close();
        }

    }

    @Override
    public void updateNotification(NotificationEntity notificationEntity) {
        try {
            session = SessionFactoryUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.update(notificationEntity);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            session.close();
        }

    }

    @Override
    public List<NotificationEntity> queryNotificationById(int id) {

        session = SessionFactoryUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from team.niit.entity.NotificationEntity n where n.id=?0";
        Query query = session.createQuery(hql);
        query.setParameter(0, id);
        List<NotificationEntity> list = query.list();
        return list;
    }

    @Override
    public List<NotificationEntity> queryAll() {
        session = SessionFactoryUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from team.niit.entity.NotificationEntity";
        Query query = session.createQuery(hql);
        List<NotificationEntity> list = query.list();
        return list;
    }

    @Override
    public List<NotificationEntity> queryAllByMajor(String major) {
        session = SessionFactoryUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from team.niit.entity.NotificationEntity n where n.major=?0";
        Query query = session.createQuery(hql);
        query.setParameter(0, major);
        List<NotificationEntity> list = query.list();
        return list;
    }

    @Override
    public boolean deleteNotificationById(int id) {
        boolean flag = false;
        Transaction tx = null;
        session = SessionFactoryUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            String hql = "delete team.niit.entity.NotificationEntity n where n.id=?0";
            Query query = session.createQuery(hql);
            query.setParameter(0, id);
            int ret = query.executeUpdate();
            tx.commit();
            if (ret > 0) {
                flag = true;
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
            return flag;
        } finally {
            session.close();
        }

    }

    @Override
    public List<NotificationEntity> queryByPageNum(int pageNo) {
        // 每页数据条数
        int everyPageMaxData = 6;
        List<NotificationEntity> list = null;
        Session se = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = se.beginTransaction();
            Query query = se.createQuery("From team.niit.entity.NotificationEntity n where n.saved=?0");
            query.setParameter(0, 0);
            query.setFirstResult((pageNo - 1) * everyPageMaxData);
            query.setMaxResults(everyPageMaxData);
            list = query.list();
            for (int i = 0; i < list.size(); i++) {
                String content = list.get(i).getContent().replaceAll(" ", "");
                String subbedContent = StrUtil.sub(content, 0, 40) + "...";
                list.get(i).setContent(subbedContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            // 关闭链接，防止连接池栈满
            se.close();
        }

        return list;

    }

    @Override
    public long countPageNum() {
        Session se = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List<Long> list = null;
        try {
            tx = se.beginTransaction();
            String hql = "select count(*) from team.niit.entity.NotificationEntity";
            Query query = se.createQuery(hql);
            list = query.list();
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            if (null != se) {
                se.close();
            }
        }
        if (list.get(0) % 6 == 0) {
            return list.get(0) / 6;
        } else if (list.get(0) % 6 != 0) {
            return list.get(0) / 6 + 1;
        } else {
            return 0;
        }
    }

    @Override
    public long countAllNum() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            String hql = "select count(*) from team.niit.entity.NotificationEntity n where n.saved=?0";
            org.hibernate.query.Query query = session.createQuery(hql);
            query.setParameter(0, 0);
            List<Long> list = query.list();
            return list.get(0);

        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            session.close();
        }
        return 0;
    }

    @Override
    public long countAllSavedNum() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            String hql = "select count(*) from team.niit.entity.NotificationEntity n where n.saved=?0";
            org.hibernate.query.Query query = session.createQuery(hql);
            query.setParameter(0, 1);
            List<Long> list = query.list();
            return list.get(0);

        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            session.close();
        }
        return 0;
    }

    @Override
    public List<NotificationEntity> querySavedByPageNum(int pageNo) {
        // 每页数据条数
        int everyPageMaxData = 6;
        List<NotificationEntity> list = null;
        Session se = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = se.beginTransaction();
            Query query = se.createQuery("From team.niit.entity.NotificationEntity n where n.saved=?0");
            query.setParameter(0, 1);
            query.setFirstResult((pageNo - 1) * everyPageMaxData);
            query.setMaxResults(everyPageMaxData);
            list = query.list();
            for (int i = 0; i < list.size(); i++) {
                String content = list.get(i).getContent().replaceAll(" ", "");
                String subbedContent = StrUtil.sub(content, 0, 40) + "...";
                list.get(i).setContent(subbedContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            // 关闭链接，防止连接池栈满
            se.close();
        }

        return list;
    }

    @Override
    public long countSavedPageNum() {
        Session se = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List<Long> list = null;
        try {
            tx = se.beginTransaction();
            String hql = "select count(*) from team.niit.entity.NotificationEntity n where n.saved=?0";
            Query query = se.createQuery(hql);
            query.setParameter(0, 1);
            list = query.list();
        } catch (Exception e) {
            e.printStackTrace();
            if (null != tx) {
                tx.rollback();
            }
        } finally {
            if (null != se) {
                se.close();
            }
        }
        if (list.get(0) % 6 == 0) {
            return list.get(0) / 6;
        } else if (list.get(0) % 6 != 0) {
            return list.get(0) / 6 + 1;
        } else {
            return 0;
        }
    }

    @Override
    public List<NotificationEntity> querySavedNotificationById(int id) {
        session = SessionFactoryUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from team.niit.entity.NotificationEntity n where n.id=?0 and n.saved=?1";
        Query query = session.createQuery(hql);
        query.setParameter(0, id);
        query.setParameter(1, 1);
        List<NotificationEntity> list = query.list();
        return list;
    }
}
